package com.linkage.order.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.order.pojo.Order;
import com.linkage.order.service.OrderService;
/**
 * @author XiaMingYu  
 */
@Controller
@RequestMapping("/orderController")
public class OrderController {
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	//@NeedLogin
	public String orderList(Model model) {
		List<Order> orders = this.orderService.queryAll();
		model.addAttribute("orders", orders);
		return "/order/list";
		}
	
	/*@RequestMapping(value = "/orderline", method = RequestMethod.GET)
	//@NeedLogin
	public String orderLine(Model model) {
		List<OrderLine> orderLines = this.orderService.queryItem();
		model.addAttribute("orderLines", orderLines);
		return "/order/desc";
		}*/
	
		@RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
		public String saveOrder(Order order){
			this.orderService.saveOrder(order);
			return "redirect:/orderlist";
		}
		
		@RequestMapping(value = "/deleteOrder/{orderId}", method = RequestMethod.GET)
		//@NeedLogin
		public String delete(Order order, @PathVariable("orderId") String orderId) {
			this.orderService.deleteOrder(orderId);
			return "redirect:/orderController/orderlist";
		}
		
		@RequestMapping(value = "/payView/{orderId}", method = RequestMethod.GET)
		//@NeedLogin
			public String payView(Model model, @PathVariable("orderId") String orderId) {
				Order order = this.orderService.loadOneOrder(orderId);
				model.addAttribute("order", order);
			return "/order/pay";
		}
		
		@RequestMapping(value = "/payOrder/{orderId}", method = RequestMethod.POST)
		//@NeedLogin
		public String pay(@PathVariable("orderId") String orderId){
			this.orderService.payOrder(orderId);
			return "redirect:/orderController/orderlist";
		}
		
		@RequestMapping(value = "/confirmView/{orderId}", method = RequestMethod.GET)
		//@NeedLogin
			public String confirmView(Model model, @PathVariable("orderId") String orderId) {
				Order order = this.orderService.loadOneOrder(orderId);
				model.addAttribute("order", order);
			return "/order/confirmView";
		}
		
		@RequestMapping(value = "/confirm/{orderId}", method = RequestMethod.POST)
		//@NeedLogin
		public String confirm(Order order){
			this.orderService.confirm(order);
			return "redirect:/orderController/orderlist";
		}
	}

